package com.adcc.foc.utils;

import com.adcc.foc.domain.ResultState;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {

    /**
     * 密码过期校验
     * @param passwordUpdateTime
     * @param passwordInvalid
     * @param passwordRemind
     * @return 0:正常，10：密码失效；11：密码提醒
     */
    public static int DateCompare(Date passwordUpdateTime,Integer passwordInvalid,Integer passwordRemind){
        if(passwordUpdateTime == null){
            return 10;
        }
        //失效时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(passwordUpdateTime);
        calendar.add(Calendar.DAY_OF_MONTH,passwordInvalid);
        Date invalidDate = calendar.getTime();
        //当前时间
        Date currentDate = new Date();
        if(currentDate.after(invalidDate)){
            return ResultState.PASSWORD_INVALID;
        }
        //提醒时间
        calendar.add(Calendar.DAY_OF_MONTH,-passwordRemind);
        Date remindTime = calendar.getTime();
        if(currentDate.after(remindTime)){
            return ResultState.PASSWORD_REMIND;
        }
        return 0;
    }

    /**
     * 获取重置密码更新时间（重置密码登陆后会提示密码即将到期）
     * @param passwordInvalid
     * @param passwordRemind
     * @return
     */
    public static Date createRestPasswordTime(Integer passwordInvalid,Integer passwordRemind){
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH,(-passwordInvalid+7));
        Date createTime = calendar.getTime();
        return createTime;
    }

    /**
     * 获取即将过期天数
     * @return
     */
    public static int getDays(Date passwordUpdateTime,Integer passwordInvalid,Integer passwordRemind){
        //失效时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(passwordUpdateTime);
        calendar.add(Calendar.DAY_OF_MONTH,passwordInvalid);

        //当前时间
        Date currentDate = new Date();
        Calendar calendar1 = new GregorianCalendar();
        calendar1.setTime(currentDate);

        int daysBetween = 0;
        while(calendar1.before(calendar)){
            calendar1.add(Calendar.DAY_OF_MONTH, 1);
            daysBetween++;
        }
        return daysBetween;
    }
}
